<template>
  <div class="login">
    <div class="min-box">
      <p>嗨购后台管理系统</p>
      <div class="box">
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
          <el-form-item prop="adminname">
            <el-input placeholder="请输入用户名" v-model="ruleForm.adminname" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" @keydown.enter="loginFn" type="password" v-model="ruleForm.password"
              autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loginFrom(ruleFormRef)">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// http://180.76.99.14/hg-admin/static/media/bg.035dc3cb90909320a094.jpg

import type { FormInstance, FormRules } from 'element-plus'
import { ElNotification } from 'element-plus'
import { reactive, ref } from 'vue'
import type { IPerson } from '../../mytype';
import service from '@/utils/service';
import { useRouter } from 'vue-router';

const route = useRouter()

const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive<IPerson>({
  adminname: '',
  password: '',
})

const rules = reactive<FormRules>({
  adminname: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

const loginFn = () => {
  service({
    url: '/admin/admin/login',
    method: 'post',
    data: {
      adminname: ruleForm.adminname,
      password: ruleForm.password
    }
  }).then((res: any) => {
    if (res.data.code === '200') {
      sessionStorage.setItem('token', res.data.data.token)
      sessionStorage.setItem('adminname', ruleForm.adminname)
      sessionStorage.setItem('checkedkeys', res.data.data.checkedkeys)

      route.push('/layout')

      ElNotification({
        message: '登录成功！',
        type: 'success',
      })
    } else if (res.data.code === '10005') {
      ElNotification({
        message: '该管理员还未被添加，请添加后再进行登录！',
        type: 'error',
      })
    } else if (res.data.code === '10003') {
      ElNotification({
        message: '用户名或密码错误，请再次核对！',
        type: 'error',
      })
    }
  })
}

const loginFrom = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid: any) => {
    if (valid) {
      loginFn()
    } else {
      console.log('error submit!')
      return false
    }
  })
}

</script>

<style scoped>
.login {
  height: 100%;
  background-image: url(http://180.76.99.14/hg-admin/static/media/bg.035dc3cb90909320a094.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
}

.login .min-box p {
  color: white;
  font-weight: 700;
  font-size: 25px;
  margin: 20px 0;
  margin-left: 75px;
}

.min-box {
  width: 400px;
  height: 300px;

}

.box {
  width: 360px;
  height: 250px;
  background-color: white;
  border-radius: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-input {
  width: 280px;
}

.el-button {
  width: 280px;
}
</style>